package cmd

import (
	"log"
	"strings"

	"github.com/spf13/cobra"
	"golang.org/x/text/language"
)

var (
	RawDataPath string

	SqlBackend    SQLBackend
	sqlBackendRaw string

	langsRaw []string
	Langs    []language.Tag
)

type SQLBackend int

const (
	POSTGRESQL SQLBackend = iota
	SQLITE
)

func (b SQLBackend) String() string {
	switch b {
	case POSTGRESQL:
		return "PostgreSQL"
	case SQLITE:
		return "SQLite"
	default:
		return "UNKNOWN"
	}
}

var rootCmd = &cobra.Command{
	Use:     "api-cache",
	Short:   "A tool to load wiktionary data to SQL database",
	Long:    `A tool to load wiktionary data to SQL database`,
	Version: "v0.1.0",
	Run: func(cmd *cobra.Command, args []string) {
		// TODO: implement command
		parseSQLBackend()
		parseLangs()
		log.Println("SQL backend:", SqlBackend.String())
		log.Println("Languages:", Langs)
	},
}

func parseSQLBackend() {
	switch strings.ToUpper(sqlBackendRaw) {
	case strings.ToUpper(POSTGRESQL.String()):
		SqlBackend = POSTGRESQL
	case strings.ToUpper(SQLITE.String()):
		SqlBackend = SQLITE
	default:
		log.Fatalf("invalid SQL backend: %s", sqlBackendRaw)
	}
}

func parseLangs() {
	// if len(langsRaw) == 0 {
	// 	Langs = []language.Tag{language.English}
	// }
	for _, lang := range langsRaw {
		tag, err := language.Parse(lang)
		if err != nil {
			log.Fatalf("failed to parse language tag: %v", err)
		}
		Langs = append(Langs, tag)
	}
}

func Execute() error {
	if err := rootCmd.Execute(); err != nil {
		return err
	}
	return nil
}

func init() {
	rootCmd.PersistentFlags().StringVar(&RawDataPath, "raw-data", "", "raw data file path")
	rootCmd.PersistentFlags().StringVar(
		&sqlBackendRaw,
		"sql-backend",
		"SQLite",
		"SQL backend, choosing from PostgreSQL or SQLite.",
	)
	rootCmd.PersistentFlags().StringSliceVar(
		&langsRaw,
		"langs",
		[]string{"en"},
		"Languages to load",
	)

	if err := rootCmd.MarkPersistentFlagRequired("raw-data"); err != nil {
		log.Fatalf("failed to mark persistent flag required: %v", err)
	}
}
